package edu.unc.ils.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;

public class ResultsParser {

    public static void main(String[] args) throws Exception
    {
        BufferedReader br = new BufferedReader(new FileReader("output_ex.txt"));
        String line;

        int vocabularySize = 48308; // # of NALT concepts;
        
        Map<String, DescriptiveStatistics> dataMap = new HashMap<String, DescriptiveStatistics>();
        
        Map<String, DescriptiveStatistics> fMap = new HashMap<String, DescriptiveStatistics>();
        
        while ((line = br.readLine()) != null) 
        {
            String[] fields = line.split(",");
            
            if (fields.length < 5)
                continue;
            
            String id = fields[0];
            String k = fields[1];
            String n = fields[2];
            String bt = fields[3];
            String nt = fields[4];
            String rt = fields[5];
            String label = k + "_" + n + "_" + bt + "_" + nt + "_" + rt;
           
            
            double numGoodType = Double.valueOf(fields[6]);
            double numFoundType = Double.valueOf(fields[7]);
            double numFoundAndGoodType = Double.valueOf(fields[8]);
            double numFoundToken = Double.valueOf(fields[9]);
            double numFoundAndGoodToken = Double.valueOf(fields[10]);
            
            double numFoundAndBadType = numFoundType - numFoundAndGoodType;
            double numBadType = vocabularySize - numGoodType;
            
            //p is the number of correct results divided by the number of all returned results
            double p = numFoundAndGoodType / numFoundType;
            
            //r is the number of correct results divided by the number of results that should have been returned
            double r = numFoundAndGoodType / numGoodType;
            

            // Get the f-measure
            double beta = 1;
            double f = fmeasure(beta, p, r);
            
            // Get the Losee measure
            double l = losee(numFoundAndGoodType, numGoodType, numFoundAndBadType, numBadType);
            
            System.out.println(label + "," + p + "," + r + "," + f + "," + l);
            
            DescriptiveStatistics data = dataMap.get(label);
            if (data == null)
                data = new DescriptiveStatistics();
            data.addValue(l);
            dataMap.put(label, data);
            
            DescriptiveStatistics fdata = fMap.get(label);
            if (fdata == null)
                fdata = new DescriptiveStatistics();
            fdata.addValue(f);
            fMap.put(label, fdata);

        }
        
        for (String label : dataMap.keySet())
        {
            DescriptiveStatistics data = dataMap.get(label);
            DescriptiveStatistics fdata = fMap.get(label);
            
            System.out.println(label + "," + data.getMean() + "," + 
                    fdata.getMean() );
        }

    }
    
    static double fmeasure(double beta, double p, double r)
    {
        //double f = 2 * (p/r);           

        double betaSq = Math.pow(beta, 1);
        double f = 0;
        if (p > 0 && r > 0)
           f = (1 + betaSq) * ( (p/r) / ((betaSq * p) + r) );
        
        return f;
    }
    
    static double losee(double numFoundAndGoodType, double numGoodType, double numFoundAndBadType, double numBadType)
    {
        double num = 0;
        if (numFoundAndGoodType > 0 && numGoodType > 0)
            num = numFoundAndGoodType/numGoodType;

        double den = 0;
        if (numFoundAndBadType > 0 && numBadType > 0)
            den = numFoundAndBadType/numBadType;
        
        double log2 = 0;
        if (num > 0 && den > 0)
        {
            double l = num/den;
        
            // Log base 2
            log2 = Math.log(l)/Math.log(2);
        }
        return log2;
    }
}
